package com.cysion.study.ui.fragment

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import com.cysion.study.R
import com.cysion.study.adapter.ClassManagesAdapter
import com.cysion.study.entity.AllResearchStuEntity
import com.cysion.study.helper.UserCache
import com.cysion.study.ktbox.base.BaseFragment
import com.cysion.study.ktbox.base.ITEM_CLICK
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.ktbox.utils.CLog
import com.cysion.study.net.ApiClient
import com.cysion.study.ui.personal.ClassManageActivity
import com.cysion.study.uibox.bar.TopBar
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.widget.ShowToast
import com.cysion.study.widget.UIHelper
import com.cysion.targetfun._subscribe
import com.scwang.smartrefresh.layout.constant.RefreshState
import kotlinx.android.synthetic.main.fragment_class_manage.*

/**
 * 普通教师-课程管理
 * Created by chu on 2019-06-30.
 */
class ClassManagesFragment:BaseFragment(){
    private lateinit var classAdapter: ClassManagesAdapter
//    private var mSourceList: MutableList<String> = mutableListOf()
    override fun getLayoutId(): Int {
        return R.layout.fragment_class_manage
    }

    override fun initView() {
//        darkTextTheme(color(R.color.main_cl))
        topbar.apply {
            initElements(TopBar.ELEMENT.HIDE,TopBar.ELEMENT.TEXT,TopBar.ELEMENT.HIDE)
            setTitle("课程管理")
        }
        initRecyclerView()
        initRefreshLayout()


    }
    private var curPage = 1

    private fun initRecyclerView() {
        rv_list.isNestedScrollingEnabled=false
        classAdapter= ClassManagesAdapter(dataList,context)
        rv_list.adapter=classAdapter
        rv_list.layoutManager=LinearLayoutManager(context)

        classAdapter.setOnTypeClickListener { obj, position, flag ->
            if (flag == ITEM_CLICK) {
//                ShowToast("查看课表")
//                context.startActivity_ex<CourseMoreActivity>()//课表
                var bundle = Bundle()
                bundle.putString("researchId",obj.cid)
                UIHelper.jump(context, ClassManageActivity::class.java,bundle)//课程列表的课列表
            }/*else if (flag == ClassManageAdapter.know) {
//                ShowToast("习题与知识点")
                context.startActivity_ex<KnowListActivity>()//习题与知识点
            } else if (flag == ClassManageAdapter.comment) {
//                ShowToast("查看评价")
                context.startActivity_ex<ClassCommentActivity>()//评价列表
            }*/

        }

    }

    //    初始化刷新控件
    private fun initRefreshLayout() {
//        smartLayout.setEnableLoadMore(false)
        smartLayout.setOnRefreshListener {
            curPage=1
            getDataList(curPage)
            smartLayout.setEnableLoadMore(true)
        }
        smartLayout.setOnLoadMoreListener {

            getDataList(curPage)
        }

        //模拟数据
       /* if (classAdapter.itemCount == 0) {
            var lists: MutableList<String> = mutableListOf("","")
            mSourceList.addAll(lists)
            classAdapter.notifyDataSetChanged()
        }*/
    }

    override fun initData() {
        super.initData()
        smartLayout.autoRefresh()
    }
    private val dataList: MutableList<AllResearchStuEntity> = mutableListOf()

    private fun getDataList(page: Int) {
        Alert.loading(context)
        ApiClient.api.getAllResearchStuList(curPage,UserCache.token).compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    stopRefresh()
                    CLog.e("@@@=" + classAdapter.itemCount)

                   /* if (classAdapter.itemCount == 0) {
                        var lists: MutableList<String> = mutableListOf("","")
                        mSourceList.addAll(lists)
                        classAdapter.notifyDataSetChanged()
                    }*/
                    if (curPage == 1) {
                        dataList.clear()
                        if (it.size == 0) {
                            multiView.showEmpty()
                        }
                    }
                    if (it.size <= 0) {
                        smartLayout.setEnableLoadMore(false)
                    } else {
                        curPage++
                        dataList.addAll(it)
                        classAdapter.notifyDataSetChanged()
                        multiView.showContent()
                    }



                }
                _onError {
                    Alert.close()
                    stopRefresh()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }
    }






    private fun stopRefresh() {
        if (smartLayout.state == RefreshState.Refreshing) {
            smartLayout.finishRefresh()
        } else if (smartLayout.state == RefreshState.Loading) {
            smartLayout.finishLoadMore(100)
        }
    }



    override fun closeMvp() {
    }
}